package com.contact.util;

import java.util.concurrent.TimeUnit;

public final class StringUtils extends org.springframework.util.StringUtils {

    private StringUtils() {
        //singleton
    }

    public static boolean isEmpty(String string) {
        return !hasText(string);
    }

    /**
     * Returns a parsed string so that only the numbers are left.
     *
     * @param str the string
     * @return the cleaned up string
     */
    public static String onlyNumbers(final String str) {
        if (str == null) {
            return null;
        }
        return str.replaceAll("[^0-9]", "");
    }

    /**
     * To be used in logging timing statements for methods.
     * @param methodName the name of the method or fragment of code called
     * @param time the time in milliseconds
     * @return formated message with the method name and the time it took displayed in minutes, seconds and milliseconds
     */
    public static String formatTimeWithMethodName(String methodName, long time) {
        long minutes = TimeUnit.MILLISECONDS.toMinutes(time);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(minutes);
        long milliseconds = time - TimeUnit.MINUTES.toMillis(minutes) - TimeUnit.SECONDS.toMillis(seconds);
    	return String.format("%s took %d min, %02d sec, %03d msec (%d abs msec)",
                methodName, minutes, seconds, milliseconds, time);
    }


}
